/**
 * 名称：创建测量
 * 作用：通过传入的类型创建对应的测量方式
 * 参数：type：Polyline、Polygon
 * start：开始，
 * end:结束
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function createMeasures(type) {
    measureType = type
    // 清空鼠标事件
    deleteHandlerFu()
    // let pointNum = pointNum
    let position = []
    let tempPoints = []
    let activeShapePoints = []
    // 开启深度检测
    viewer.scene.globe.depthTestAgainstTerrain = false
    // 创建场景的HTML canvas元素
    // 记录实体id
    let entitieId = null
    // 记录绘制线段实体
    let pointline = null
    // 记录测量米实体
    let pointLabel =null
    // 记录角度实体
    let Angle =null
    // 记录线段的第一个点
    let drop = null
    // 记录最后一次创建的线段
    let finallyCreate = []
    // 记录角度点位
    let anglePointPosition = []
    // 角度文字信息
    let angleData = null
    tempEntities =[]
    // 存储每次创建的测量信息
    let measurementData = []
    // 配置实体id信息
    measurementType = type+"&"+measurementStorageData.size
    // 每次创建都清空创建数量
    measurementLength = 0
    // 判断是否要删除最后一次创建
    let deletestate = false
    // 记录每次创建的点
    let point = null
    let midpoint = new Cesium.Cartesian3()
    // 监听鼠标结束
    let timer
    // 模型坐标
    let newPosition = null
    switch (type) {
        // 绘制线
        case 'Polyline':
            // 取消鼠标双击事件
            viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
            // 监听鼠标移动
            handler.setInputAction(function (movement) {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    deletestate = true
                    // 位置信息组装
                    newPosition = positionAssembly(movement.endPosition)
                    console.log('多段线信息查看：', newPosition)
                    if (Cesium.defined(floatingPoint)) {
                        if (Cesium.defined(newPosition)) {
                            floatingPoint.position.setValue(newPosition)
                            activeShapePoints.pop()
                            activeShapePoints.push(newPosition)
                        }
                    }
                    // 获取位置信息
                    if (newPosition === false){
                        console.error('多段线创建点失败，位置转换信息失败：', newPosition)
                    }else {
                        position = newPosition
                        console.log('开始创建多段线')
                        // 存在超过一个点时
                        if (entitieId) {
                            // 当都创建过之后就根据移动改变值即可
                            if (pointline&&pointLabel){
                                let pointLength = getLength(drop, position)
                                // 计算终点
                                let midPosition = getMidpoint(drop, position)
                                pointline.polyline.positions = [drop, position]
                                midpoint  = Cesium.Cartesian3.midpoint(position,drop,new Cesium.Cartesian3())
                                // pointLabel.position = midPosition
                                pointLabel.position = midpoint
                                pointLabel.label.text = pointLength + 'm'
                                if(tempPoints.length > 1) {
                                    let angleVale = computeCZ(drop, tempPoints[tempPoints.length - 2], position)
                                    Angle.label.text = angleVale + '°'
                                }

                            }else {
                                // 绘制线
                                // 计算距离
                                let pointLength = getLength(drop, position)
                                // 计算终点
                                let midPosition = getMidpoint(drop, position)
                                // 绘制线段
                                pointline = drawPolyline([drop, position])
                                // 绘制米
                                // pointLabel = addLabel(midPosition, pointLength+"m")
                                midpoint  = Cesium.Cartesian3.midpoint(position,drop,new Cesium.Cartesian3())
                                pointLabel = addLabel(midpoint, pointLength+"m")
                                if(tempPoints.length > 1){
                                    let angleVale = computeCZ(drop,tempPoints[tempPoints.length - 2],position)
                                    Angle = addLabel(drop, angleVale+"°",1)
                                }
                                let data = {
                                    // 点
                                    drop:point,
                                    // 线
                                    line:pointline,
                                    // 数据
                                    data:pointLabel,
                                    Angle: Angle
                                }
                                measurementData.push(data)
                            }
                            finallyCreate = [pointline, pointLabel]
                        }
                    }
                }, 100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            // 左键单击开始画线
            handler.setInputAction(function (click) {
                console.log('查看点击的参数：',click)
                deletestate = false
                measurementLength = measurementData.length + 1
                entitieId = null
                // 若上面对位置的解析失败了就不进入到下面的操作，因为会有断链的情况
                if (newPosition === false){
                    console.error('多段线创建点失败，位置转换信息失败：', newPosition)
                }else {
                    let earthPosition = newPosition
                    // 进行模型与点图的点击判断
                    if (activeShapePoints.length === 0) {
                        // 创建点
                        floatingPoint = drawPoint(earthPosition)
                    }
                    // 获取位置信息
                    position = earthPosition
                    tempPoints.push(position) // 记录点位
                    pointNum += 1
                    let tempLength = tempPoints.length // 记录点数
                    // 调用绘制点的接口
                    point = drawPointLabel(tempPoints[tempPoints.length - 1], JSON.stringify(pointNum))
                    entitieId = point
                    drop = position
                    tempEntities.push(point)
                    // 存在超过一个点时
                    if (tempLength > 1) {
                        tempEntities.push(pointline) // 保存记录
                        tempEntities.push(pointLabel)
                        pointline = null
                        pointLabel = null
                    }
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
            // 右键单击结束画线
            handler.setInputAction(function (click) {
                activeShapePoints.pop()
                viewer.entities.remove(activeShapePoints)
                viewer.entities.remove(floatingPoint)
                // 删除最后一个创建
                if (finallyCreate.length === 0 || finallyCreate.length === 2 && deletestate){
                    viewer.entities.remove(finallyCreate[1])
                }
                if (finallyCreate.length>0&&deletestate){
                    viewer.entities.remove(finallyCreate[0])
                    viewer.entities.remove(finallyCreate[1])
                    //根据下标删除最后一个
                    measurementData.splice(measurementData.length-1,1);
                }
                tempPoints = [] // 清空点位记录
                // 存储信息
                measurementStorageData.set("Polyline&"+measurementStorageData.size,measurementData)
                console.log("查看数据Polyline:",measurementStorageData)
                handler.destroy()
                handler = null
                floatingPoint = undefined
                activeShape = undefined
                activeShapePoints = []
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
            break
        // 绘制面
        case 'Polygon':
            // 取消鼠标双击事件
            viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
            // 监听鼠标移动
            handler.setInputAction(function (movement) {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    console.log('查看触摸信息：', movement)
                    deletestate = true
                    // 位置信息组装
                    newPosition = positionAssembly(movement.endPosition)
                    if (newPosition === false) {
                        console.error('面积创建点失败，位置转换信息失败：', newPosition)
                    }else {
                        console.log('开始绘制面积：', newPosition)
                        if (Cesium.defined(floatingPoint)) {
                            if (Cesium.defined(newPosition)) {
                                floatingPoint.position.setValue(newPosition)
                                activeShapePoints.pop()
                                activeShapePoints.push(newPosition)
                            }
                        }
                        // 获取位置信息
                        position = newPosition
                        if (entitieId){
                            if (pointline){
                                pointline.polyline.positions = [drop, position]
                            }else {
                                // 绘制线
                                pointline = drawPolyline([drop, position])
                                let data = {
                                    // 点
                                    drop:point,
                                    // 线
                                    line:pointline,
                                    // 数据
                                    data:""
                                }
                                measurementData.push(data)
                            }
                            finallyCreate = [pointline]
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            // 左键单击开始画线
            handler.setInputAction(function (click) {
                measurementLength = measurementData.length + 1
                deletestate = false
                entitieId = null
                // 若上面对位置的解析失败了就不进入到下面的操作，因为会有断链的情况
                if (newPosition === false){
                    console.error('面积创建点失败，位置转换信息失败：', newPosition)
                }else {
                    // 获取位置信息
                    position = newPosition
                    drop = position
                    tempPoints.push(position) // 记录点位
                    // 判断是否点击的是模型
                    if (activeShapePoints.length === 0) {
                        floatingPoint = drawPoint(position)
                        // 实时创建多边形
                        // const dynamicPositions = new Cesium.CallbackProperty(function () {
                        //     return new Cesium.PolygonHierarchy(activeShapePoints)
                        // }, false)
                        // activeShape = drawPolygon(dynamicPositions)
                    }
                    activeShapePoints.push(position)
                    let tempLength = tempPoints.length // 记录点数
                    pointNum += 1
                    // 调用绘制点的接口
                    point = drawPointLabel(tempPoints[tempPoints.length - 1], JSON.stringify(pointNum))
                    entitieId = point
                    tempEntities.push(point)
                    // 存在超过一个点时
                    if (tempLength > 1) {
                        tempEntities.push(pointline) // 保存记录
                        pointline = null
                    }
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
            // 右键单击结束画面
            handler.setInputAction(function (click) {
                console.error('面积测量点击了右键')
                measurementLength = measurementData.length + 1
                // 选择一个椭球或地图
                let cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid)
                if (cartesian) {
                    let tempLength = tempPoints.length
                    if (tempLength < 3) {
                        alert('闭合操作需要至少3个点嗷')
                        return
                    } else {
                        console.log('开始停止多边形创建:', finallyCreate.length>0&&deletestate)
                        if (finallyCreate.length>0&&deletestate){
                            console.log('开始删除面积中的最后一个点')
                            measurementLength = measurementLength -1
                            //根据下标删除最后一个
                            measurementData.splice(measurementData.length-1,1);
                            viewer.entities.remove(finallyCreate[0])
                        }
                        // 闭合最后一条线
                        let pointline = drawPolyline([tempPoints[tempPoints.length - 1],tempPoints[0]])
                        tempEntities.push(pointline)
                        let polygon = drawPolygon(tempPoints)
                        // 获取多边形的中心点
                        // var boundingSphere = Cesium.BoundingSphere.fromVertices(polygon.polygon.hierarchy.getValue())
                        // // 获取边界的中心点
                        // var center = boundingSphere.center
                        let pointArea = getArea(tempPoints)
                        let area = addArea(JSON.stringify(pointArea), tempPoints[tempPoints.length - 1])
                        console.log('查看点位信息：', measurementData)
                        let data = {
                            // 点
                            // drop:measurementData[0].drop,// 因为闭合点的位置就是0点，所以需要取第0位的数据
                            drop:point,// 因为闭合点的位置就是0点，所以需要取第0位的数据
                            // 线
                            line:pointline,
                            // 数据
                            data:area,
                            // 多边形
                            polygon:polygon
                        }
                        measurementData.push(data)
                        console.log('完成面积绘制，数据为：', measurementData)
                        // 存储信息
                        measurementStorageData.set("Polygon&"+measurementStorageData.size,measurementData)
                        console.log("查看数据Polygon:",measurementStorageData)
                        tempEntities.push(tempPoints)
                        handler.destroy()
                        handler = null
                    }
                }
                activeShapePoints.pop()
                viewer.entities.remove(activeShapePoints)
                viewer.entities.remove(floatingPoint)
                floatingPoint = undefined
                activeShapePoints = []
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK)
            break
        // 角度
        case "Angle":
            // 取消鼠标双击事件
            viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
            // 监听鼠标移动
            handler.setInputAction(function (movement) {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 位置信息组装
                    newPosition = positionAssembly(movement.endPosition)
                    if (newPosition === false) {
                        console.error('角度创建点失败，位置转换信息失败：', newPosition)
                    }else {
                        console.log('测试是否有继续往下走2')
                        if (Cesium.defined(floatingPoint)) {
                            if (Cesium.defined(newPosition)) {
                                floatingPoint.position.setValue(newPosition)
                                activeShapePoints.pop()
                                activeShapePoints.push(newPosition)
                            }
                        }
                        if (entitieId){
                            // // 获取位置信息
                            position = newPosition
                            if (pointline){
                                pointline.polyline.positions = [drop, position]
                                if (anglePointPosition.length === 2 && pointLabel){
                                    let angle = pointAngle(anglePointPosition[0],anglePointPosition[1],position)+"°"
                                    pointLabel.label.text = angle
                                }
                            }else {// 绘制线段
                                // 创建
                                pointline = drawPolyline([drop, position])
                                // 生成点
                                if (anglePointPosition.length === 2) {
                                    // 计算坐标
                                    let angle = pointAngle(anglePointPosition[0],anglePointPosition[1],position)+"°"
                                    pointLabel = addLabel(anglePointPosition[1],angle)
                                }
                                let data = {
                                    // 点
                                    drop:point,
                                    // 线
                                    line:pointline,
                                    // 数据
                                    data:pointLabel
                                }
                                measurementData.push(data)
                            }
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            // 左键单击开始画线
            handler.setInputAction(function (click) {
                measurementLength +=1
                entitieId = null
                // 若上面对位置的解析失败了就不进入到下面的操作，因为会有断链的情况
                if (newPosition === false){
                    console.error('角度创建点失败，位置转换信息失败：', newPosition)
                }else {
                    console.log('测试是否有继续往下走1')
                    let earthPosition = newPosition
                    if (activeShapePoints.length === 0) {
                        floatingPoint = drawPoint(earthPosition)
                    }
                    activeShapePoints.push(earthPosition)
                    // 获取位置信息
                    position = earthPosition
                    anglePointPosition.push(position)
                    drop = position
                    tempPoints.push(position) // 记录点位
                    let tempLength = tempPoints.length // 记录点数
                    pointNum += 1
                    // 调用绘制点的接口
                    point = drawPointLabel(tempPoints[tempPoints.length - 1], JSON.stringify(pointNum))
                    entitieId = point
                    tempEntities.push(point)
                    // 存在超过一个点时
                    if (tempLength > 1) {
                        tempEntities.push(pointline) // 保存记录
                        pointline = null
                    }
                    if (anglePointPosition.length === 3){
                        // 存储信息
                        measurementStorageData.set("Angle&"+measurementStorageData.size,measurementData)
                        console.log("查看数据Angle:",measurementStorageData)
                        // 结束创建
                        // 删除最后一个实体
                        viewer.entities.remove(floatingPoint)
                        handler.destroy()
                        handler = null
                    }
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
            break
        // 直线
        case "Line":
            // 取消鼠标双击事件
            viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
            // 监听鼠标移动
            handler.setInputAction(function (movement) {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 位置信息组装
                    newPosition = positionAssembly(movement.endPosition)
                    console.log('查看返回的数据:', newPosition)
                    if (newPosition === false) {
                        console.error('直线创建点失败，位置转换信息失败：', newPosition)
                    }else {
                        if (Cesium.defined(floatingPoint)) {
                            if (Cesium.defined(newPosition)) {
                                floatingPoint.position.setValue(newPosition)
                                activeShapePoints.pop()
                                activeShapePoints.push(newPosition)
                            }
                        }
                        if (entitieId){
                            // // 获取位置信息
                            position = newPosition
                            if (pointline){
                                let pointLength = getLength(drop, position)
                                // 计算终点
                                let midPosition = getMidpoint(drop, position)
                                pointline.polyline.positions = [drop, position]
                                midpoint  = Cesium.Cartesian3.midpoint(position,drop,new Cesium.Cartesian3())
                                pointLabel.position = midpoint
                                pointLabel.label.text = pointLength + 'm'
                            }else {
                                // 绘制线
                                // 计算距离
                                let pointLength = getLength(drop, position)
                                // 计算终点
                                let midPosition = getMidpoint(drop, position)
                                // 绘制线段
                                pointline = drawPolyline([drop, position])
                                midpoint  = Cesium.Cartesian3.midpoint(position,drop,new Cesium.Cartesian3())
                                // 绘制米
                                pointLabel = addLabel(midpoint, pointLength+"m")
                            }
                        }
                    }
                }, 100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            // 左键单击开始画线
            handler.setInputAction(function (click) {
                measurementLength +=1
                entitieId = null
                // 若上面对位置的解析失败了就不进入到下面的操作，因为会有断链的情况
                if (newPosition === false){
                    console.error('直线创建点失败，位置转换信息失败：', newPosition)
                }else {
                    let earthPosition = newPosition
                    if (activeShapePoints.length === 0) {
                        floatingPoint = drawPoint(earthPosition)
                    }
                    activeShapePoints.push(earthPosition)
                    // 获取位置信息
                    position = earthPosition
                    anglePointPosition.push(position)
                    drop = position
                    tempPoints.push(position) // 记录点位
                    let tempLength = tempPoints.length // 记录点数
                    pointNum += 1
                    // 调用绘制点的接口
                    point = drawPointLabel(tempPoints[tempPoints.length - 1], JSON.stringify(pointNum))
                    entitieId = point
                    tempEntities.push(point)
                    // 存在超过一个点时
                    if (tempLength > 1) {
                        tempEntities.push(pointline) // 保存记录
                    }
                    if (anglePointPosition.length === 2){
                        // 存储信息
                        let data = {
                            // 点
                            drop:point,
                            // 线
                            line:pointline,
                            // 数据
                            data:pointLabel
                        }
                        measurementData.push(data)
                        measurementStorageData.set("Line&"+measurementStorageData.size,measurementData)
                        console.log("查看数据Line:",measurementStorageData)
                        // 结束创建
                        // 删除最后一个实体
                        viewer.entities.remove(floatingPoint)
                        handler.destroy()
                        handler = null
                    }
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
            break
        // 垂直
        case "vertical":
            let point_temp = null
            let pointEnd = null
            let dashedLine = null
            // 两点的x,y不发生改变，只改变z,来进行垂直的绘制
            // 取消鼠标双击事件
            viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
            // 监听鼠标移动
            handler.setInputAction(function (movement) {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 位置信息组装
                    newPosition = positionAssembly(movement.endPosition)
                    if (newPosition === false) {
                        console.error('垂直创建点失败，位置转换信息失败：', newPosition)
                    }else {
                        if (Cesium.defined(floatingPoint)) {
                            if (Cesium.defined(newPosition)) {
                                floatingPoint.position.setValue(newPosition)
                                activeShapePoints.pop()
                                activeShapePoints.push(newPosition)
                            }
                        }
                        if (entitieId){
                            // // 获取位置信息
                            position = newPosition
                            point_temp =returnHeightPosition(drop, position)
                            if (pointline){
                                let pointLength = getHeight(drop, point_temp)
                                // 计算终点
                                let midPosition = getMidpoint(drop, point_temp)
                                pointline.polyline.positions = [drop, point_temp]
                                pointEnd.position  = point_temp
                                dashedLine.polyline.positions = [point_temp, position]
                                midpoint  = Cesium.Cartesian3.midpoint(point_temp,drop,new Cesium.Cartesian3())
                                pointLabel.position = midpoint
                                pointLabel.label.text = pointLength + 'm'
                                // 创建末点
                            }else {
                                // 绘制线
                                // 计算距离
                                let pointLength = getHeight(drop, point_temp)
                                // 计算终点
                                let midPosition = getMidpoint(drop, position)
                                // 绘制线段
                                pointline = drawPolyline([drop, point_temp])
                                midpoint  = Cesium.Cartesian3.midpoint(point_temp,drop,new Cesium.Cartesian3())
                                // 绘制米
                                pointLabel = addLabel(midpoint, pointLength+"m")
                                measurementLength +=1
                                pointEnd = drawPointLabel(point_temp, JSON.stringify(pointNum))
                                dashedLine = drawDashedLine([point_temp, position])
                            }
                        }
                    }
                }, 100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            // 左键单击开始画线
            handler.setInputAction(function (click) {
                measurementLength +=1
                entitieId = null
                // 若上面对位置的解析失败了就不进入到下面的操作，因为会有断链的情况
                if (newPosition === false){
                    console.error('垂直创建点失败，位置转换信息失败：', newPosition)
                }else {
                    let earthPosition = newPosition
                    if (activeShapePoints.length === 0) {
                        floatingPoint = drawPoint(earthPosition)
                    }
                    activeShapePoints.push(earthPosition)
                    // 获取位置信息
                    position = earthPosition
                    anglePointPosition.push(position)
                    drop = position
                    tempPoints.push(position) // 记录点位
                    let tempLength = tempPoints.length // 记录点数
                    pointNum += 1
                    // 调用绘制点的接口
                    point = drawPointLabel(tempPoints[tempPoints.length - 1], JSON.stringify(pointNum))
                    entitieId = point
                    tempEntities.push(point)
                    // 存在超过一个点时
                    if (tempLength > 1) {
                        tempEntities.push(pointline) // 保存记录
                    }
                    if (anglePointPosition.length === 2){
                        // 存储信息
                        let data = {
                            // 起点
                            drop:point,
                            // 终点
                            dropEnd:pointEnd,
                            // 直线线
                            line:pointline,
                            // 虚线
                            dashedLine: dashedLine,
                            // 数据
                            data: pointLabel
                        }
                        measurementData.push(data)
                        measurementStorageData.set("vertical&"+measurementStorageData.size,measurementData)
                        console.log("查看数据Line:",measurementStorageData)
                        // 结束创建
                        // 删除最后一个实体
                        viewer.entities.remove(floatingPoint)
                        handler.destroy()
                        handler = null
                    }
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
            break
        case "synthesis":
            let point_temp_syn = null
            let pointEnd_syn = null
            let dashedLine_syn = null
            let dashedLine_two = null
            // 起点到终点的距离
            let beginning_end = null
            // 起点 夹角
            let angle_start = null
            // 第三和 终点的距离
            let pedal_end = null
            // 终点夹角
            let angle_end = null
            // 两点的x,y不发生改变，只改变z,来进行垂直的绘制
            // 取消鼠标双击事件
            viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
            // 监听鼠标移动
            handler.setInputAction(function (movement) {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 位置信息组装
                    newPosition = positionAssembly(movement.endPosition)
                    if (newPosition === false) {
                        console.error('垂直创建点失败，位置转换信息失败：', newPosition)
                    }else {
                        if (Cesium.defined(floatingPoint)) {
                            if (Cesium.defined(newPosition)) {
                                floatingPoint.position.setValue(newPosition)
                                activeShapePoints.pop()
                                activeShapePoints.push(newPosition)
                            }
                        }
                        if (entitieId){
                            // // 获取位置信息
                            position = newPosition
                            point_temp_syn =returnHeightPosition(drop, position)
                            if (pointline){
                                // let pointLength = getHeight(drop, point_temp_syn)
                                // 计算终点
                                let midPosition = getMidpoint(drop, point_temp_syn)
                                pointline.polyline.positions = [drop, point_temp_syn]
                                pointEnd_syn.position  = point_temp_syn
                                dashedLine_syn.polyline.positions = [point_temp_syn, position]
                                // 起三
                                midpoint  = Cesium.Cartesian3.midpoint(point_temp_syn,drop,new Cesium.Cartesian3())
                                let point_temp_syn_drop = Cesium.Cartesian3.distance(point_temp_syn,drop).toFixed(2)
                                pointLabel.position = midpoint
                                pointLabel.label.text = point_temp_syn_drop + 'm'
                                dashedLine_two.polyline.positions = [drop,position]
                                // 起终点距离
                                let start_end = Cesium.Cartesian3.distance(drop,position).toFixed(2)
                                beginning_end.label.text = start_end + 'm'
                                midpoint  = Cesium.Cartesian3.midpoint(drop,position,new Cesium.Cartesian3())
                                beginning_end.position = midpoint
                                // 三终点距离
                                let point_temp_syn_end = Cesium.Cartesian3.distance(point_temp_syn,position).toFixed(2)
                                midpoint  = Cesium.Cartesian3.midpoint(point_temp_syn,position,new Cesium.Cartesian3())
                                pedal_end.label.text = point_temp_syn_end + 'm'
                                pedal_end.position = midpoint
                                angle_start.label.text = computeCZ(drop,point_temp_syn,position)+"°"
                                angle_end.label.text = computeCZ(position,drop,point_temp_syn)+"°"
                                angle_end.position = position
                                // 创建末点
                            }else {
                                // 起点 drop  第三点 point_temp_syn   终点position
                                // 绘制线
                                // 计算距离
                                // let pointLength = getHeight(drop, point_temp_syn)
                                // // 计算终点
                                // let midPosition = getMidpoint(drop, position)
                                // 绘制线段 起三
                                pointline = drawDashedLine([drop, point_temp_syn])
                                // 起点到第三点的中心点
                                midpoint  = Cesium.Cartesian3.midpoint(point_temp_syn,drop,new Cesium.Cartesian3())
                                // 起 三 的距离
                                let point_temp_syn_drop = Cesium.Cartesian3.distance(point_temp_syn,drop).toFixed(2)
                                // 绘制米 起 三
                                pointLabel = addLabel(midpoint, point_temp_syn_drop+"m",3)
                                // 起 终 的线
                                dashedLine_two = drawPolyline([drop, position])
                                //起 终 的中点
                                midpoint  = Cesium.Cartesian3.midpoint(position,drop,new Cesium.Cartesian3())
                                // 起终点距离
                                let start_end = Cesium.Cartesian3.distance(drop,position).toFixed(2)
                                // 起 终
                                beginning_end = addLabel(midpoint, start_end+"m",3)
                                // 终 三的中点
                                midpoint  = Cesium.Cartesian3.midpoint(position,point_temp_syn,new Cesium.Cartesian3())
                                // 三终点距离
                                let point_temp_syn_end = Cesium.Cartesian3.distance(point_temp_syn,position).toFixed(2)
                                // 终 三
                                pedal_end = addLabel(midpoint, point_temp_syn_end+"m",3)
                                measurementLength +=1
                                pointEnd_syn = drawPointLabel(point_temp_syn, JSON.stringify(pointNum))
                                // 终 三 的线
                                dashedLine_syn = drawDashedLine([point_temp_syn, position])
                                // 起点角度
                                angle_start = addLabel(drop,  computeCZ(drop,point_temp_syn,position)+"°",3)
                                angle_end = addLabel(position,  computeCZ(position,drop,point_temp_syn)+"°",3)
                            }
                        }
                    }
                }, 100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            // 左键单击开始画线
            handler.setInputAction(function (click) {
                measurementLength +=1
                entitieId = null
                // 若上面对位置的解析失败了就不进入到下面的操作，因为会有断链的情况
                if (newPosition === false){
                    console.error('垂直创建点失败，位置转换信息失败：', newPosition)
                }else {
                    let earthPosition = newPosition
                    if (activeShapePoints.length === 0) {
                        floatingPoint = drawPoint(earthPosition)
                    }
                    activeShapePoints.push(earthPosition)
                    // 获取位置信息
                    position = earthPosition
                    anglePointPosition.push(position)
                    drop = position
                    tempPoints.push(position) // 记录点位
                    let tempLength = tempPoints.length // 记录点数
                    pointNum += 1
                    // 调用绘制点的接口
                    point = drawPointLabel(tempPoints[tempPoints.length - 1], JSON.stringify(pointNum))
                    entitieId = point
                    tempEntities.push(point)
                    // 存在超过一个点时
                    if (tempLength > 1) {
                        tempEntities.push(pointline) // 保存记录
                    }
                    if (anglePointPosition.length === 2){
                        // 存储信息
                        let data = {
                            // 起点
                            drop:point,
                            // 终点
                            dropEnd:pointEnd_syn,
                            //  起 三
                            line:pointline,
                            // 终 三
                            dashedLine: dashedLine_syn,
                            //  起终
                            dashedLine_tow: dashedLine_two,
                            // 数据 起三
                            data: pointLabel,
                            // 终三
                            data_two: pedal_end,
                            // 起终
                            data_tre: beginning_end,
                            // 终点度
                            angel_end: angle_end,
                            // 起点角度
                            angle_start: angle_start
                        }
                        measurementData.push(data)
                        measurementStorageData.set("synthesis&"+measurementStorageData.size,measurementData)
                        console.log("查看数据Line:",measurementStorageData)
                        // 结束创建
                        // 删除最后一个实体
                        viewer.entities.remove(floatingPoint)
                        handler.destroy()
                        handler = null
                    }
                }
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
            break
    }
}

/**
 * 名称：离计算
 * 作用：计算空间中两点的距离
 * 参数：start：开始，end:结束
 * start：开始，
 * end:结束
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function getLength(start, end) {
    // 将起点与终点位置信息从笛卡尔坐标形式转换为Cartographic形式
    let startCartographic = Cesium.Cartographic.fromCartesian(start)
    let endCartographic = Cesium.Cartographic.fromCartesian(end)
    // 初始化测地线
    let geodesic = new Cesium.EllipsoidGeodesic()
    // 设置测地线起点和终点，EllipsoidGeodesic中setEndPoints常与surfaceDistance搭配使用
    geodesic.setEndPoints(startCartographic, endCartographic)
    return (geodesic.surfaceDistance).toFixed(2)
}

/**
 * 名称：计算中点
 * 作用：计算空间两点计算中点函数
 * 参数：start：开始，end:结束
 * start：开始，
 * end:结束
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function getMidpoint(start, end) {
    let startPoint = Cesium.Cartographic.fromCartesian(start)
    let endPoint = Cesium.Cartographic.fromCartesian(end)
    let geodesic = new Cesium.EllipsoidGeodesic()
    geodesic.setEndPoints(startPoint, endPoint)
    let geoPoint = geodesic.interpolateUsingFraction(0.5)
    return Cesium.Ellipsoid.WGS84.cartographicToCartesian(geoPoint)
}

/**
 * 名称：创建实体标签
 * 作用：创建实体标签
 * 参数：midPoint：开始，labelLength:结束
 * midPoint：坐标，
 * labelLength：距离
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function addLabel(midPoint, labelLength, type = 0) {
    let id
    if(type === 0){
        id = measurementType+"_label"+"_"+measurementLength
    }else if(type === 1){
        id = measurementType+"_angel"+"_"+measurementLength
    }else if (type === 3){
        let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        let maxPos = chars.length
        let pwd = ''
        for (i = 0; i < 10; i++) {
            pwd += chars.charAt(Math.floor(Math.random() * maxPos))
        }
        id = measurementType + pwd
    }
    return viewer.entities.add({
        id: id,
        name: '中点',
        position: midPoint,
        label: {
            text: labelLength,
            font: '20px sans-serif',
            fillColor: Cesium.Color.WHITE,
            outlineWidth: 2,
            backgroundColor: Cesium.Color.BLACK,
            showBackground: true,
            style: Cesium.LabelStyle.FILL,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            disableDepthTestDistance: Number.POSITIVE_INFINITY
        }
    })
}

/**
 * 名称：测量空间面积
 * 作用：通过点位测量空间面积
 * 参数：from：开始，to:结束
 * from：坐标，
 * to：距离
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function Bearing(from, to) {
    let fromCartographic = Cesium.Cartographic.fromCartesian(from)
    let toCartographic = Cesium.Cartographic.fromCartesian(to)
    let lat1 = fromCartographic.latitude
    let lon1 = fromCartographic.longitude
    let lat2 = toCartographic.latitude
    let lon2 = toCartographic.longitude
    let angle = -Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2))
    if (angle < 0) {
        angle += Math.PI * 2.0
    }
    return angle
}

// 角度
/**
 * 名称：测量角度
 * 作用：通过传入的三个点来计算其角度
 * 参数：point1：开始，point2:结束，point3:结束
 * point1：坐标1
 * point2：坐标2
 * point3：坐标3
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function pointAngle(point1, point2, point3) {
    let bearing21 = Bearing(point2, point1)
    let bearing23 = Bearing(point2, point3)
    let angle = bearing21 - bearing23
    if (angle < 0) {
        angle += Math.PI * 2.0
    }
    return angle
}

/**
 * 名称：获取多边形
 * 作用：通过传入的三个点来计算其角度
 * 参数：positions：开始
 * positions：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function getArea(positions) {
    let res = 0
    for (let i = 0; i < positions.length - 2; i++) {
        let j = (i + 1) % positions.length
        let k = (i + 2) % positions.length
        let totalAngle = pointAngle(positions[i], positions[j], positions[k])
        let tempLength1 = getLength(positions[j], positions[0])
        let tempLength2 = getLength(positions[k], positions[0])
        res += tempLength1 * tempLength2 * Math.sin(totalAngle) / 2
    }
    res = res.toFixed(2)
    res = parseFloat(res)
    return Math.abs(res)
}

/**
 * 名称：绘制多边形
 * 作用：通过传入的信息来进行多边形的绘制
 * 参数：area：开始，positions
 * area：坐标1
 * positions：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function addArea(area, positions) {
    console.log('面积测量结构：', area, positions)
    return viewer.entities.add({
        id: measurementType+"_area"+"_"+measurementLength,
        name: '多边形面积',
        position: positions,
        label: {
            text: area + 'm2',
            font: '20px sans-serif',
            fillColor: Cesium.Color.WHITE,
            outlineWidth: 2,
            backgroundColor: Cesium.Color.BLACK,
            showBackground: true,
            style: Cesium.LabelStyle.FILL,
            // 调整绘制高度
            // pixelOffset: new Cesium.Cartesian2(60, -60),
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            disableDepthTestDistance: Number.POSITIVE_INFINITY
        }
    })
}

/**
 * 名称：绘制点
 * 作用：通过传入的三个点来计算其角度
 * 参数：position：开始，pointNum
 * position：坐标1
 * pointNum：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function drawPointLabel(position, pointNum) {
    // 本质上就是添加一个点的实体
    return viewer.entities.add({
        id: measurementType+"_pointlabel"+"_"+measurementLength,
        name: '点几何对象',
        position: position,
        point: {
            color: Cesium.Color.WHEAT,
            pixelSize: 5,
            outlineWidth: 3,
            disableDepthTestDistance: Number.POSITIVE_INFINITY, //
            // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND // 规定贴地
        }
        // // 创建公告牌
        // label: {
        //     text: pointNum,
        //     font: '30px sans-serif',
        //     fillColor: Cesium.Color.WHITE,
        //     outlineWidth: 2,
        //     backgroundColor: Cesium.Color.BLACK,
        //     showBackground: true,
        //     style: Cesium.LabelStyle.FILL,
        //     verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        //     horizontalOrigin: Cesium.HorizontalOrigin.CENTER
        // }
    })
}

/**
 * 名称：绘制点
 * 作用：通过传入的三个点来计算其角度
 * 参数：position：开始
 * position：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function drawPoint(position) {
    // 本质上就是添加一个点的实体
    return viewer.entities.add({
        id: measurementType+"_point"+"_"+measurementLength,
        position: position,
        point: {
            color: Cesium.Color.WHEAT,
            pixelSize: 5,
            outlineWidth: 3,
            disableDepthTestDistance: Number.POSITIVE_INFINITY,
            // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND // 规定贴地
        }
    })
}

/**
 * 名称：绘制多段线
 * 作用：通过传入的三个点来计算其角度
 * 参数：position：开始
 * position：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function drawPolyline(positions) {
    console.log('开始绘制线段0：', positions.length)
    if (positions.length < 1) return
    console.log('开始绘制线段1：', positions.length)
    return viewer.entities.add({
        id: measurementType+"_polyline"+"_"+measurementLength,
        name: '线几何对象',
        polyline: {
            positions: positions,
            width: 2,
            material: new Cesium.PolylineGlowMaterialProperty({
                // eslint-disable-next-line new-cap
                color: Cesium.Color.WHEAT
            }),
            depthFailMaterial: new Cesium.PolylineGlowMaterialProperty({
                // eslint-disable-next-line new-cap
                color: Cesium.Color.WHEAT
            }),
            // clampToGround: true // 贴地
            zIndex: 1
        }
    })
}

/**
 * 名称：绘制虚线
 * 作用：通过传入的参数绘制一条虚线
 * 参数：position：坐标
 * position：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function drawDashedLine(positions){
    if (positions.length < 1) return
    var dashedLine = viewer.entities.add({
        id: measurementType+"_dashedLine"+"_"+measurementLength,
        polyline: {
            positions: positions,
            width: 3,
            material: new Cesium.PolylineDashMaterialProperty({
                color: Cesium.Color.YELLOW,
                dashLength: 20 //短划线长度
            }),
            depthFailMaterial: new Cesium.PolylineGlowMaterialProperty({
                // eslint-disable-next-line new-cap
                color: Cesium.Color.WHEAT
            })
        }
    })
    return dashedLine
}

/**
 * 名称：绘制多边形
 * 作用：通过传入的三个点来计算其角度
 * 参数：position：开始
 * position：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function drawPolygon(positions) {
    console.log('开始绘制多边形0：', positions.length)
    if (positions.length < 2) return
    console.log('开始绘制多边形1：', positions.length)
    return viewer.entities.add({
        id: measurementType+"_polygon"+"_"+measurementLength,
        name: '面几何对象',
        polygon: {
            hierarchy: positions,
            width: 5.0,
            // eslint-disable-next-line new-cap
            material: new Cesium.ColorMaterialProperty(
                Cesium.Color.WHEAT.withAlpha(0.4)
            ),
            // 给予多边形高度，不然就会是贴在模型上的现象
            perPositionHeight: true,
            zIndex: 2
        }
    })
}

/**
 * 名称：清除实体
 * 作用：通过传入的三个点来计算其角度
 * 参数：position：开始
 * position：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function clearAllDrawn() {
    deleteHandlerFu()
    //值清楚在测量中创建过的实体
    // 循环所有的实体，将包含测量的所有元素进行删除
    for (const key of measurementStorageData.keys()) {
        for (const index in measurementStorageData.get(key)) {
            for (const i in measurementStorageData.get(key)[index]) {
                console.log('获取到内部的值：', measurementStorageData.get(key)[index][i])
                viewer.entities.remove(measurementStorageData.get(key)[index][i])
            }
        }
    }
    for (let i = 0; i < viewer.entities._entities._array.length; i++) {
        if (viewer.entities._entities._array[i].id.indexOf("&")!==-1){
            viewer.entities.remove(viewer.entities._entities._array[i])
        }
    }
    console.log("删除所有实体")
    // 将存储数据置为空
    measurementStorageData.clear()
    tempEntities = []
    pointNum = 0
    // viewer.entities.removeAll()
}

/**
 * 名称：测量编辑
 * 作用：通过点击点来进行测量的编辑
 * 参数：type：开始
 * position：坐标1
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 */
function editMeasurement(entitieId) {
    console.log("传入的信息entitieId：",entitieId)
    let entitieArr = entitieId.split("_")
    // 类型判断
    let type = entitieArr[0].split("&")[0]
    switch (type) {
        // 面积
        case "Polygon":
            editPolygon(entitieId)
            break
        // 多段线
        case "Polyline":
            editPolyline(entitieId)
            break
        // 角度
        case "Angle":
            editAngle(entitieId)
            break
        // 直线
        case "Line":
            editLine(entitieId)
            break
        // 锤线
        case "vertical":
            editVertical(entitieId)
            break
        case "synthesis":
            editSynthesis(entitieId)
            break
    }
}

/**
 * 名称：面积编辑
 * 作用：对绘制的面进行编辑
 * 参数：entitieId：实体id
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 * 备注：面积当下标为0的时候就改变收尾两点
 */
function editPolygon(entitieId) {
    console.log("开始进入面积编辑", entitieId)
    let entitieArr = entitieId.split("_")
    // 通过点信息获取到选段信息,需要将下标-1，因为是从1开始的
    let index = entitieArr[entitieArr.length-1] -1
    let measurementArr = measurementStorageData.get(entitieArr[0])
    // 获取末点数据，用于更改面积的实时数据
    let data = measurementArr[measurementArr.length-1]
    let end  = null
    let position = null
    let start = null
    let startdata = null
    let enddata = null
    let polygon = null
    let areaData = null
    // 判断移动结束
    let timer
    switch (index) {
        case 0:
            // 当为0的时候需要改变收尾两点
            console.error('查看点击的点1：',index)
            // 获取第一条直线
            start = measurementArr[index]
            console.log('查看面积测量数据：', start)
            startdata =  start.line.polyline.positions._value[1]
            // 获取最后一条直线
            end = measurementArr[measurementArr.length-1]
            polygon = data.polygon
            areaData = data.data
            enddata = end.line.polyline.positions._value[0]
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        // 位置信息组装
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('面积编辑失败，没有获取到位置数据信息:', position)
                        }else {
                            dragPointLpick.id.position = position;
                            // 起点改变,第一点不变，后面一点改变
                            start.line.polyline.positions = [position, startdata]
                            // 终点改变，第一点不改，后面改变
                            end.line.polyline.positions = [enddata, position]
                            // 修改点位置
                            start.drop.position = position
                            // 更改多边形
                            var polygonValue = polygon.polygon.hierarchy.getValue().positions
                            polygonValue[0] = position
                            polygon.polygon.hierarchy = polygonValue
                            // 更改面积的值
                            var areaValue = getArea(polygonValue)
                            areaData.label.text = areaValue + 'm'
                            areaData.position =  polygonValue[polygonValue.length -1]
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            break
        default:
            console.error('查看点击的点2：',index)
            // 当为0的时候需要改变收尾两点
            // 获取第一条直线
            start = measurementArr[index-1]
            console.log('查看点击点位信息：', start)
            startdata =  start.line.polyline.positions._value[0]
            // 获取最后一条直线
            end = measurementArr[index]
            console.log('查看选中的数据：measurementArr', measurementArr)
            polygon = data.polygon
            areaData = data.data
            enddata = end.line.polyline.positions._value[1]
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        // 位置信息组装
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('面积编辑失败，没有获取到位置数据信息:', position)
                        }else {
                            dragPointLpick.id.position = position;
                            // 起点改变,第一点不变，后面一点改变
                            start.line.polyline.positions = [startdata, position]
                            // 终点改变，第一点不改，后面改变
                            end.line.polyline.positions = [position, enddata]
                            // 更改多边形
                            var polygonValue = polygon.polygon.hierarchy.getValue().positions
                            polygonValue[index] = position
                            polygon.polygon.hierarchy = polygonValue
                            // 更改面积的值
                            var areaValue = getArea(polygonValue)
                            areaData.label.text = areaValue + 'm'
                            areaData.position = polygonValue[polygonValue.length -1]
                            // 修改点位置
                            end.drop.position = position
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            break
    }
}

/**
 * 名称：多段线编辑
 * 作用：对绘制的多段线进行编辑
 * 参数：entitieId：实体id
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 * 备注：只有三种形式
 */
function editPolyline(entitieId) {
    console.log("开始进入多段线编辑")
    let entitieArr = entitieId.split("_")
    // 通过点信息获取到选段信息,需要将下标-1，因为是从1开始的
    let index = entitieArr[entitieArr.length-1] -1
    let measurementArr = measurementStorageData.get(entitieArr[0])
    console.log('查看直线编辑下标：', index, measurementArr)
    let data = null
    let data_tow = null
    let data_tre = null
    let end  = null
    let position = null
    let start = null
    let timer
    let midpoint
    let angel_value
    let angel_value_tre
    let angel_end
    // 首先判断id下标是第0问还是最后一位
    switch (index) {
        case 0:
            // 首点，第一点的位置改变，第一条直线改变首点发生改变，只需要改变值的数据
            // 当是第一位的时候,获取下标为0的数据，改变线的第一点，数据的信息
            data = measurementArr[index]
            console.log('查看首位的位置信息：', data)
            end = data.line.polyline.positions._value[1]
            // 后面的数据
            if(measurementArr.length >= 2) {
                data_tow = measurementArr[index + 1]
                angel_end = data_tow.line.polyline.positions._value[1]
            }
            // 移动
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){

                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('多段线编辑失败，没有获取到位置数据信息:', position)
                        }else {
                            dragPointLpick.id.position = position;
                            let pointLength = getLength(position, end)
                            // // 计算终点
                            let midPosition = getMidpoint(position, end)
                            // 改变直线位置
                            data.line.polyline.positions = [position, end]
                            midpoint  = Cesium.Cartesian3.midpoint(position,end,new Cesium.Cartesian3())
                            // 改变第一点的位置
                            data.drop.position = position
                            // 改变值的位置
                            data.data.position = midpoint
                            // 改变值的数据
                            data.data.label.text = pointLength + 'm'
                            if(measurementArr.length >= 2) {
                                angel_value = computeCZ(end, position, angel_end)
                                data_tow.Angle.label.text = angel_value + '°'
                            }
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            break
        case measurementArr.length:
            console.error('这个点是末尾点')
            // 末端，最后的点的位置改变，最后的直线改变末点发生改变
            data = measurementArr[index-1]
            end = data.line.polyline.positions._value[0]
            // 后面的数据
            if(measurementArr.length >= 2) {
                data_tow = measurementArr[index - 2]
                angel_end = data_tow.line.polyline.positions._value[0]
            }
            // 移动
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('多段线编辑失败，没有获取到位置数据信息:', position)
                        }else {
                            dragPointLpick.id.position = position;
                            let pointLength = getLength(end, position)
                            // // 计算终点
                            let midPosition = getMidpoint(end, position)
                            midpoint  = Cesium.Cartesian3.midpoint(position,end,new Cesium.Cartesian3())
                            data.line.polyline.positions = [end, position]
                            data.data.position = midpoint
                            data.data.label.text = pointLength + 'm'
                            if(measurementArr.length >= 2) {
                                angel_value = computeCZ(end, position, angel_end)
                                data.Angle.label.text = angel_value + '°'
                            }
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            break
        default:
            // 当是中间的时候,移动前一条线的末尾坐标，后一条的起点坐标
            // 起点数据
            start = measurementArr[index-1]
            console.log('起点数据：', start)
            let startdata =  start.line.polyline.positions._value[0]
            // 终点数据
            end = measurementArr[index]
            // 后面的数据
            if(measurementArr.length > 2){
                if(measurementArr.length  > index + 1){
                    data_tow = measurementArr[index + 1]
                    angel_end = data_tow.line.polyline.positions._value[1]
                }
                if(index - 2 >= 0){
                    data_tre = measurementArr[index - 2]
                    angel_value_tre = data_tre.line.polyline.positions._value[0]
                }

            }
            console.log('查看中间点位的信息：', end)
            let enddata = end.line.polyline.positions._value[1]
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('多段线编辑失败，没有获取到位置数据信息:', position)
                        }else {
                            dragPointLpick.id.position = position;
                            // 开始直线，直线中第一点不变，后面一点改变
                            let startPointLength = getLength(startdata, position)
                            // 计算中点
                            let startMidPosition = getMidpoint(startdata, position)
                            start.line.polyline.positions = [startdata, position]
                            midpoint  = Cesium.Cartesian3.midpoint(position,startdata,new Cesium.Cartesian3())
                            // 改变值的信息
                            start.data.position = midpoint
                            start.data.label.text = startPointLength + 'm'
                            // 末端直线，第一点改变，后面一点不变
                            let endPointLength = getLength(position, enddata)
                            let endMidPosition = getMidpoint(position, enddata)
                            end.line.polyline.positions = [position, enddata]
                            midpoint  = Cesium.Cartesian3.midpoint(position,enddata,new Cesium.Cartesian3())
                            // 改变值的信息
                            end.data.position = midpoint
                            end.data.label.text = endPointLength + 'm'
                            angel_value = computeCZ(position,startdata, enddata)
                            end.Angle.label.text = angel_value + '°'
                            end.Angle.position = position
                            if(measurementArr.length > 2){
                                if(measurementArr.length  > index + 1){
                                    angel_value = computeCZ(enddata, position, angel_end)
                                    data_tow.Angle.label.text = angel_value + '°'
                                }
                                if(index - 2 >= 0){
                                    angel_value = computeCZ(startdata, position, angel_value_tre)
                                    start.Angle.label.text = angel_value + '°'
                                }
                            }
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

            break
    }
}

/**
 * 名称：角度编辑
 * 作用：对绘制的角度进行编辑
 * 参数：entitieId：实体id
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 * 备注：
 */
function editAngle(entitieId) {
    console.log("开始进入角度编辑")
    let entitieArr = entitieId.split("_")
    // 通过点信息获取到选段信息,需要将下标-1，因为是从1开始的
    let index = entitieArr[entitieArr.length-1] -1
    let measurementArr = measurementStorageData.get(entitieArr[0])
    console.log("measurementArr:",measurementArr)
    let data = null
    let end  = null
    let position = null
    let start = null
    let timer
    switch (index) {
        case 0:
            // 改变第一个点的位置，改变第一条线的末端
            // 获取数据
            start = measurementArr[index]
            // 记录末点坐标
            end = start.line.polyline.positions._value[1]
            // 移动
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('角度编辑失败，没有获取到位置数据信息:', position)
                        }else {
                            dragPointLpick.id.position = position;
                            start.line.polyline.positions = [position, end]
                            // 改变角度信息drop
                            let two = measurementArr[0].line.polyline.positions._value[1]
                            let three = measurementArr[1].line.polyline.positions._value[1]
                            let angle = pointAngle(position,two,three)+"°"
                            measurementArr[1].data.label.text = angle
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            break
        case measurementArr.length:
            // 改变末点的位置，该变最后一条线的第一点
            // 获取数据
            end = measurementArr[index-1]
            // 记录起点数据
            start = end.line.polyline.positions._value[0]
            // 移动
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('角度编辑失败，没有获取到位置数据信息:', position)
                        }else {
                            dragPointLpick.id.position = position;
                            end.line.polyline.positions = [start, position]
                            // 改变角度信息drop
                            let one = measurementArr[0].line.polyline.positions._value[0]
                            let two = measurementArr[1].line.polyline.positions._value[0]
                            let angle = pointAngle(one,two,position)+"°"
                            measurementArr[1].data.label.text = angle
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            break
        default:
            // 获取起点数据
            start = measurementArr[index-1]
            let startdata = start.line.polyline.positions._value[0]

            // 获取终点数据
            end = measurementArr[index]
            // 记录末点坐标
            let enddata = end.line.polyline.positions._value[1]
            // 移动
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('角度编辑失败，没有获取到位置数据信息:', position)
                        }else {
                            dragPointLpick.id.position = position;
                            // 起点线
                            start.line.polyline.positions = [startdata, position]
                            // 终点线
                            end.line.polyline.positions = [position, enddata]
                            measurementArr[1].data.position = position
                            // 计算角度
                            let angle = pointAngle(startdata,position,enddata)+"°"
                            measurementArr[1].data.label.text = angle
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            break
    }
}

/**
 * 名称：直线编辑
 * 作用：对绘制的直线进行编辑
 * 参数：entitieId：实体id
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 * 备注：
 */
function editLine(entitieId) {
    console.log("开始进入直线编辑")
    let entitieArr = entitieId.split("_")
    // 通过点信息获取到选段信息,需要将下标-1，因为是从1开始的
    let index = entitieArr[entitieArr.length-1] -1
    let measurementArr = measurementStorageData.get(entitieArr[0])
    let midpoint
    console.log("measurementArr:",measurementArr)
    let data = null
    let end  = null
    let position = null
    let start = null
    let timer
    switch (index) {
        case 0:
            // 当是第一位的时候,获取下标为0的数据，改变线的第一点，数据的信息
            data = measurementArr[0]
            end = data.line.polyline.positions._value[1]
            // 移动
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('直线编辑失败，没有获取到位置数据信息:', position)
                        }else{
                            dragPointLpick.id.position = position;
                            let pointLength = getLength(position, end)
                            // // 计算终点
                            let midPosition = getMidpoint(position, end)
                            midpoint = Cesium.Cartesian3.midpoint(position,end,new Cesium.Cartesian3())
                            data.line.polyline.positions = [position, end]
                            data.data.position = midpoint
                            data.data.label.text = pointLength + 'm'
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            break
        case measurementArr.length:
            data = measurementArr[0]
            end = data.line.polyline.positions._value[0]
            // 移动
            handler.setInputAction(e => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(function (){
                    // 移动点数据
                    if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                        position = positionAssembly(e.endPosition)
                        if (position === false) {
                            console.error('直线编辑失败，没有获取到位置数据信息:', position)
                        }else {
                            dragPointLpick.id.position = position;
                            let pointLength = getLength(end, position)
                            // // 计算终点
                            let midPosition = getMidpoint(end, position)
                            midpoint = Cesium.Cartesian3.midpoint(position,end,new Cesium.Cartesian3())
                            data.line.polyline.positions = [end, position]
                            data.data.position = midpoint
                            data.data.label.text = pointLength + 'm'
                        }
                    }
                },100)
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            break
    }
}


/**
 * 名称：垂直编辑
 * 作用：对绘制的垂直线段进行编辑
 * 参数：entitieId：实体id
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 * 备注：
 */
function editVertical(entitieId) {
    console.log("开始进入垂直编辑")
    // alert('暂时不支持编辑')
    let entitieArr = entitieId.split("_")
    // 通过点信息获取到选段信息,需要将下标-1，因为是从1开始的
    let index = entitieArr[entitieArr.length-1] -1
    let measurementArr = measurementStorageData.get(entitieArr[0])
    let midpoint
    if (entitieArr[1] + '_' + entitieArr[2] === 'pointlabel_3') {
        // 拖动的时候直线的末端需要改变，点的高度需要改变，虚线的两点都需要改变
        let timer
        let data = null
        let end  = null
        let position = null
        handler.setInputAction(e => {
            if (timer) {
                clearTimeout(timer)
            }
            timer = setTimeout(function (){
                if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                    position = positionAssembly(e.endPosition)
                    if (position === false){
                        console.log('垂直编辑失败，没有获取到位置信息：', position)
                    }else {
                        let positionData = measurementArr[0].line.polyline.positions._value
                        let positionEnd = returnHeightPosition(positionData[0], position)
                        // 改变线的位置信息
                        measurementArr[0].line.polyline.positions = [positionData[0], positionEnd]
                        // 2.改变终点点位置
                        measurementArr[0].dropEnd.position = positionEnd
                        // // 3.改变虚线信息
                        measurementArr[0].dashedLine.polyline.positions = [positionEnd, position]
                        midpoint = Cesium.Cartesian3.midpoint(positionData[0],positionEnd,new Cesium.Cartesian3())
                        // // 改变数据信息
                        let height = getHeight(positionData[0], positionEnd)
                        measurementArr[0].data.position = midpoint
                        measurementArr[0].data.label.text = height + 'm'
                        // 修改拖动的点信息
                        measurementArr[0].drop.position = position
                    }
                }
            },100)
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    }else {
        alert('请选择末点进行编辑')
    }
    return
}

/**
 * 名称：垂直编辑
 * 作用：对绘制的垂直线段进行编辑
 * 参数：entitieId：实体id
 * 状态：
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128178560
 * 负责人：
 * 备注：
 */
function editSynthesis(entitieId) {
    console.log("开始进入垂直编辑")
    // alert('暂时不支持编辑')
    let entitieArr = entitieId.split("_")
    // 通过点信息获取到选段信息,需要将下标-1，因为是从1开始的
    let index = entitieArr[entitieArr.length-1] -1
    let measurementArr = measurementStorageData.get(entitieArr[0])
    console.log(measurementArr,'measurementArr')
    let midpoint
    if (entitieArr[1] + '_' + entitieArr[2] === 'pointlabel_3') {
        // 拖动的时候直线的末端需要改变，点的高度需要改变，虚线的两点都需要改变
        let timer
        let data = null
        let end  = null
        let position = null
        handler.setInputAction(e => {
            if (timer) {
                clearTimeout(timer)
            }
            timer = setTimeout(function (){
                if (dragPointLeftDownFlag === true && dragPointLpick != null) {
                    position = positionAssembly(e.endPosition)
                    if (position === false){
                        console.log('垂直编辑失败，没有获取到位置信息：', position)
                    }else {
                        let positionData = measurementArr[0].line.polyline.positions._value
                        let positionEnd = returnHeightPosition(positionData[0], position)
                        // 改变线的位置信息
                        measurementArr[0].line.polyline.positions = [positionData[0], positionEnd]
                        // 2.改变终点点位置
                        measurementArr[0].dropEnd.position = positionEnd
                        // // 3.改变虚线信息
                        measurementArr[0].dashedLine.polyline.positions = [positionEnd, position]
                        midpoint = Cesium.Cartesian3.midpoint(positionData[0],positionEnd,new Cesium.Cartesian3())
                        // // 改变数据信息
                        // let height = getHeight(positionData[0], positionEnd)
                        measurementArr[0].data.position = midpoint
                        measurementArr[0].data.label.text = Cesium.Cartesian3.distance(positionData[0], positionEnd).toFixed(2) + 'm'
                        // 修改拖动的点信息
                        measurementArr[0].drop.position = position
                        measurementArr[0].dashedLine_tow.polyline.positions = [positionData[0],position]
                        midpoint = Cesium.Cartesian3.midpoint(positionEnd,position,new Cesium.Cartesian3())
                        measurementArr[0].data_two.position = midpoint
                        measurementArr[0].data_two.label.text = Cesium.Cartesian3.distance(positionEnd,position).toFixed(2) + 'm'
                        midpoint = Cesium.Cartesian3.midpoint(positionData[0],position,new Cesium.Cartesian3())
                        measurementArr[0].data_tre.position = midpoint
                        measurementArr[0].data_tre.label.text = Cesium.Cartesian3.distance(positionData[0],position).toFixed(2) + 'm'
                        measurementArr[0].angel_end.label.text = computeCZ(position,positionEnd,positionData[0])+"°"
                        measurementArr[0].angel_end.position = position
                        measurementArr[0].angle_start.label.text = computeCZ(positionData[0],position,positionEnd)+"°"
                    }
                }
            },100)
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    }else {
        alert('请选择末点进行编辑')
    }
    return
}

/**
 * 名称：组装位置信息
 * 作用：根据传入的经纬度返回对应饿位置信息
 * 参数：entitieId：实体id
 * 状态：
 * 网址：
 * 负责人：
 * 备注：
 */
function positionAssembly(position){
    let positionData = getCatesian3FromPX(position)
    console.log('查看当前点位信息::：', positionData)
    return positionData
}

/**
 * 名称：测量拖拽实体执行方法
 * 说明：通过点击实体点获取实体信息然后进行拖动
 * 参数：
 * 状态：已完成
 * 负责人：/
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128391607
 */
// 开始拖拽
function  startDrag() {
    console.log("开始移动")
    deleteHandlerFu()
    handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
    leftDownAction()
    leftUpAction()
}

/**
 * 名称：实体点信息获取
 * 说明：通过点击获取实体信息，并锁定相机
 * 参数：
 * 状态：已完成
 * 负责人：/
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128391607
 */
function leftDownAction() {
    handler.setInputAction(e => {
        let picks = viewer.scene.pick(e.position);
        console.log('查看信息picks------>：', picks)
        if (Cesium.defined(picks) && (picks.id.id)) {
            dragPointLpick = picks
            dragPointLeftDownFlag = true;
            viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
            console.error('开始拖拽查看数据是否正确', picks.id.id)
            // 将实体id传给测量
            // 在这里要进行判断，因为选中的可能是线段，并不是点，当选中的是线段的时候就会出现变化的线段错误的情况(必须拖动点才可以)
            if (picks.id.id.indexOf("_") !== -1 && picks.id.id.split('_')[picks.id.id.split('_').length - 2] === 'pointlabel'){
                console.error('开始拖拽', picks.id.id)
                // 改变选中的颜色
                picks.id.point.color = Cesium.Color.GREEN
                // 记录点击的点信息，在鼠标案件抬起的时候恢复原色
                recordMeasurementPoints = picks.id
                editMeasurement(picks.id.id)
            }else {
                recordMeasurementPoints = null
                console.error('没有选中点')
            }
        }
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
}


/**
 * 名称：鼠标抬起事件
 * 说明：拖拽实体点解锁相机
 * 参数：
 * 状态：已完成
 * 负责人：/
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128391607
 */
function leftUpAction() {
    handler.setInputAction(e => {
        if (dragPointLeftDownFlag === true && dragPointLpick != null) {
            dragPointLeftDownFlag = false;
            viewer.scene.screenSpaceCameraController.enableRotate = true;//解锁相机
            destroyDragAndDrop()
        }
    }, Cesium.ScreenSpaceEventType.LEFT_UP);
}

/**
 * 名称：销毁移动事件
 * 说明：将拖拽事件进行销毁并进行颜色的操作
 * 参数：
 * 状态：已完成
 * 负责人：/
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128391607
 */
function destroyDragAndDrop() {
    handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
    if (recordMeasurementPoints) {
        recordMeasurementPoints.point.color = Cesium.Color.WHEAT
        recordMeasurementPoints = null
    }
}

/**
 * 名称：获取两点之间的高度
 * 说明：根据传入的两点进行两点之间高度的计算
 * 参数：positionsone：起点, positionstwo：终点
 * 状态：已完成
 * 负责人：/
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128391607
 */
function getHeight(positionsone, positionstwo){
    let height_temp = 0;
    let cartographic = Cesium.Cartographic.fromCartesian(positionsone);
    let cartographic1 = Cesium.Cartographic.fromCartesian(positionstwo);
    height_temp = cartographic1.height - cartographic.height;
    return height_temp.toFixed(2);
}

/**
 * 名称：返回两点的高度坐标
 * 说明：通过传入的两点返回两点之间的高度坐标
 * 参数：positionsone：起点, positionstwo：终点
 * 状态：已完成
 * 负责人：/
 * 网址：https://blog.csdn.net/qq_40323256/article/details/128391607
 */
function returnHeightPosition(positionsone, positionstwo){
    let point1cartographic = Cesium.Cartographic.fromCartesian(positionsone);
    let point2cartographic = Cesium.Cartographic.fromCartesian(positionstwo);
    let point_temp = Cesium.Cartesian3.fromDegrees(Cesium.Math.toDegrees(point1cartographic.longitude),Cesium.Math.toDegrees(point1cartographic.latitude),point2cartographic.height);
    return point_temp
}

/**
 * 名称：返回两条线段的夹角
 * 说明：通过传入的三点返回两线之间的夹角
 * 参数：p1：点1, p2：点2 p3：点3
 * 状态：已完成
 * 负责人：/
 * 网址：
 */
function computeCZ (p1, p2, p3) {
    // 得到 p1p2 的方向向量
    var c1 = Cesium.Cartesian3.subtract(p2, p1, new Cesium.Cartesian3());
    var c2 = Cesium.Cartesian3.subtract(p3, p1, new Cesium.Cartesian3());
    // 计算两向量的夹角
    var angle = Cesium.Cartesian3.angleBetween(c1, c2);
    // var dis = Cesium.Cartesian3.distance(p1, p3);
    // console.log(Math.cos(angle))
    // dis = dis * Math.cos(angle);
    // var nor = Cesium.Cartesian3.normalize(c1, new Cesium.Cartesian3());
    // var newC = Cesium.Cartesian3.multiplyByScalar(nor, dis, new Cesium.Cartesian3());
    // var res = Cesium.Cartesian3.add(newC, p1, new Cesium.Cartesian3());
    // console.log(angle,dis,nor,newC,res)
    return ((angle * 180) / Math.PI).toFixed(2);
}
